<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="参数名称搜索" style="width: 200px;" class="filter-item" clearable />
      <!-- 选择app-->
      <el-select class="app-select" v-model="listQuery.appCode" filterable placeholder="请选择app">
        <el-option
          v-for="item in appList"
          :key="item.name"
          :label="item.fullname"
          :value="item.name">
        </el-option>
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查找
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        新建系统参数
      </el-button>
    </div>
    <el-table :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="参数名">
        <template slot-scope="scope">
          <span>{{ scope.row.paramKey }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="参数描述">
        <template slot-scope="scope">
          <span>{{ scope.row.misc }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="参数值">
        <template slot-scope="scope">
          <span>{{ scope.row.paramValue }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNumber"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <CreateAppSystemSetting
      ref="CreateAppSystemSettingForm"
      :text-map="textMap"
      :dialog-status="dialogStatus"
      :dialog-form-visible.sync="dialogFormVisible"
      @on-fresh-list="getList"
    />

  </div>
</template>

<script>
import * as apiAppSystemSetting from '@/api/app_system_setting'
import * as apiApp from '@/api/app'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
// 当然你也可以为了方便使用，将它注册到全局
import permission from '@/directive/permission/index.js' // 权限判断指令
import CreateAppSystemSetting from '@/views/app_system_setting/createAppSystemSetting'
import { MessageBox } from 'element-ui'

export default {
  name: 'AppSystemSetting',
  components: { Pagination, CreateAppSystemSetting },
  directives: { waves, permission },
  filters: {
  },
  data() {
    return {
      appList: [],
      list: null,
      total: 0,
      listQuery: { // 查询列表时的参数对象
        pageNumber: 1,
        pageSize: 50,
        name: undefined,
        appCode: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      }
    }
  },
  created() {
    this.getAppList()
  },
  mounted() {
    // 测试用
    // var that = this
    // setTimeout(function() {
    //   that.dialogFormVisible = true
    // }, 5000)
  },
  beforeDestroy() { // 当路由切换到其他页面时触发
    // alert('离开用户管理界面')
  },
  methods: {
    async getAppList() {// 获取app列表数据
      const { content }=await apiApp.getList({ // 查询列表时的参数对象
        pageNumber: 1,
        pageSize: 500
      })
      this.appList = content.list
      if (this.appList.length > 0) {
        this.listQuery.appCode = this.appList[0].name
        this.getList()
      }
    },
    async getList() { // 获取列表数据
      const { content } = await apiAppSystemSetting.getList(this.listQuery)
      this.list = content.list
      this.total = content.totalCounts
    },
    handleFilter() {
      this.listQuery.pageNumber = 1
      this.getList()
    },
    handleCreate() {
      this.$refs['CreateAppSystemSettingForm'].clearFormData() // 直接调用子组件的method, 很方便
      this.$refs['CreateAppSystemSettingForm'].setAppCode(this.listQuery.appCode) // 直接调用子组件的method, 很方便
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['CreateAppSystemSettingForm'].$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      console.log('row--->', row)
      const currentRow = Object.assign({}, row)
      this.$refs['CreateAppSystemSettingForm'].setCurrentRow(currentRow)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['CreateAppSystemSettingForm'].$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(row) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const deleteData = Object.assign({}, row) // copy obj
        apiAppSystemSetting.deleteAppSystemSetting(deleteData.id).then(() => {
          // 刷新列表数据
          this.getList()
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped>
  .app-select {
    position: relative;
    top: -4px;
  }

</style>
